<?php
namespace app\common\model;

use think\Model;
use think\Session;

class Article extends Model
{
    protected $insert = ['publish_time'];

    protected static function init()
    {
        parent::init();

        self::event('before_insert', function ($article) {
            $take_publish_time = $article->take_publish_time;
            $keyword = $article->keyword;
            if ($keyword) {
                $article->keyword = str_replace('，', ',',$keyword);
            }
            if ($take_publish_time) {
                $article->status = 0;
                $article->take_publish_time = strtotime($take_publish_time);
            }
        });
        
        self::event('before_update', function ($article) {
            if(isset($article->keyword)){
                $keyword = $article->keyword;
                    if ($keyword) {
                        $article->keyword = str_replace('，', ',',$keyword);
                    }
            }
        });
    }
    
    
    /**
     * 文章作者
     * @param $value
     * @return mixed
     */
    protected function setAuthorAttr($value)
    {
        return $value ? $value : Session::get('admin_name');
    }

    /**
     * 反转义HTML实体标签
     * @param $value
     * @return string
     */
    protected function setContentAttr($value)
    {
        return htmlspecialchars_decode($value);
    }

  
    /**
     * 创建时间
     * @return bool|string
     */
    protected function setCreateTimeAttr()
    {
        return date('Y-m-d H:i:s');
    }
    
    
}